import React from 'react';
import {
  EyeOutlined,
} from '@ant-design/icons';

const getConsultColumns = (props: any) => {
  const consultColumns = [
    {
      title: 'Title',
      dataIndex: 'projectName',
      key: 'projectName',
      width: '20%',
    },
    {
      title: 'Project stage',
      dataIndex: 'projectPhaseName',
      key: 'projectPhaseName',
      width: '16.96%',
    },
    {
      title: 'Country/Region',
      dataIndex: 'countryName',
      key: 'countryName',
      width: '16.96%',
    },
    {
      title: 'Industry',
      dataIndex: 'industryName',
      key: 'industryName',
      width: '16.07%',
    },
    {
      title: 'Investment',
      dataIndex: 'totalInvestment',
      key: 'totalInvestment',
      width: '17.32%',
    },
    {
      title: 'Project Details',
      dataIndex: 'fileUrl',
      key: 'fileUrl',
      render: (text: any) => <a onClick={() => props.handleViewFile(text)}><EyeOutlined /></a>,
    },
  ];

  return consultColumns;
};

const getCaseColumns = (props: any) => {
  const caseColumns = [
    {
      title: 'Case name',
      dataIndex: 'caseName',
      key: 'caseName',
      width: '20%',
    },
    // {
    //   title: '案例概述',
    //   dataIndex: 'prostage',
    //   key: 'prostage',
    //   width: '16.96%',
    // },
    // {
    //   title: 'Country/Region',
    //   dataIndex: 'country',
    //   key: 'country',
    //   width: '16.96%',
    // },
    // {
    //   title: 'Industry',
    //   dataIndex: 'industry',
    //   key: 'industry',
    //   width: '16.07%',
    // },
    {
      title: 'Project Details',
      dataIndex: 'fileUrl',
      key: 'fileUrl',
      render: (text: any) => <a onClick={() => props.handleViewFile(text)}><EyeOutlined /></a>,
    },
  ];

  return caseColumns;
};


const getUrlSearch = (name: string) => {
  // 未传参，返回空
  if (!name) return null;
  // 查询参数：先通过search取值，如果取不到就通过hash来取
  let after = window.location.search;
  after = after.substr(1) || window.location.hash.split('?')[1];
  // 地址栏URL没有查询参数，返回空
  if (!after) return null;
  // 如果查询参数中没有"name"，返回空
  if (!after.includes(name)) return null;

  const reg = new RegExp(`(^|&)${name}=([^&]*)(&|$)`);
  // 当地址栏参数存在中文时，需要解码，不然会乱码
  // eslint-disable-next-line @typescript-eslint/prefer-regexp-exec
  const r = decodeURI(after).match(reg);
  // 如果url中"name"没有值，返回空
  if (!r) return null;

  return r[2];
};

const detailTitle = [
  { key: '', value: 'organizationName', isTitle: true },
  { key: 'Institution Introduction:', value: 'orgnisationIntroduction' },
  { key: 'Project Performance:', value: 'accomplishments' },
  { key: 'Country/Region:', value: 'countryName' },
  { key: 'Headquarters address:', value: 'headquartersOfficeAddress' },
  { key: 'Institution Type:', value: 'orgTypeName' },
  { key: 'Main Area:', value: 'specialityName' },
  { key: 'Focus Industry:', value: 'followIndustryName' },
  { key: 'Focus Country/Region:', value: 'followCountryName' },
  { key: 'Institution Website:', value: 'officialWebsite' },
  { key: 'Project Contact:', value: 'contact' },
  { key: 'Phone Number:', value: 'phone' },
  { key: 'Email:', value: 'email' },
];

const getDetail = (detailData: any) => {
  const detailFinalData: Array<any> = [];
  detailTitle.forEach((item: any) => {
    const info = { key: null as any, value: null as any };
    info.key = item.key;
    info.value = detailData[item.value];
    detailFinalData.push({ ...info, isTitle: item.isTitle });
  });

  return detailFinalData;
};

export { getConsultColumns, getCaseColumns, getUrlSearch, getDetail };
